﻿// Unity built-in shader source. Copyright (c) 2016 Unity Technologies. MIT license (see license.txt)

Shader "Custom/Qx2DLight/Sprites/EdgeLighting-Sprite"
{
    Properties
    {
        [PerRendererData] _MainTex ("Sprite Texture", 2D) = "white" {}
        _Color ("Tint", Color) = (1,1,1,1)
        
        [MaterialToggle] PixelSnap ("Pixel snap", Float) = 0
        [HideInInspector] _RendererColor ("RendererColor", Color) = (1,1,1,1)
        [HideInInspector] _Flip ("Flip", Vector) = (1,1,1,1)
        [PerRendererData] _AlphaTex ("External Alpha", 2D) = "white" {}
        [PerRendererData] _EnableExternalAlpha ("Enable External Alpha", Float) = 0

        
         _EdgeShadowScale ("EdgeShadowScale", Float) = 1
         _EdgeShadowMax ("EdgeShadowMax", Float) = 0.01
        // _EdgeLightMax ("EdgeLightMax", Float) = 2

         _BlurDistance ("BlurDistance", Float) = 1
         
        // _ScreenLightTex ("Screen ShadowTex", 2D) = "black" {}

         _DirectWeightFade("DirectWeightFade", Range(0,1)) = 0
		[HideInInspector] _LightSpriteFlag("LightFlag", Float) = 0
    }

    SubShader
    {
        Tags
        {
            "Queue"="Transparent"
            "IgnoreProjector"="True"
            "RenderType"="Transparent"
            "PreviewType"="Plane"
            "CanUseSpriteAtlas"="True"
			"QxLightingSprite" = "True"
        }

        Cull Off
        Lighting Off
        ZWrite Off
        Blend One OneMinusSrcAlpha

        Pass //0
        {
        CGPROGRAM
            #pragma vertex SpriteVert
            #pragma fragment SpriteFrag2
            #pragma target 2.0
            #pragma multi_compile_instancing
            #pragma multi_compile _ PIXELSNAP_ON
            #pragma multi_compile _ ETC1_EXTERNAL_ALPHA
           

            #define SCREEN_LIGHT
        #include "UnitySprites.cginc"
        /*        
        struct v2f
        {
            float4 vertex   : SV_POSITION;
            fixed4 color    : COLOR;
            float2 texcoord : TEXCOORD0;
            UNITY_VERTEX_OUTPUT_STEREO
        };*/


        float4 _SunScreenPos;
        sampler2D _MaskTex;
        sampler2D _ScreenLightTex;

        float _EdgeShadowScale;
        float _BlurDistance;
        float _EdgeShadowMax;
        float _EdgeLightMax;
        float _EdgeShadowMin;
        float _IsShadow;
        float _DirectWeightFade;

        float4 _IndirectColor;
        float4 _DirectColor;


        half SampleSpriteTexture5ForA(float2 uv, float offset)
        {           
           // return SampleSpriteTexture(uv).a;
            float samp = SampleSpriteTexture(uv).a;

            samp += SampleSpriteTexture (uv + float2(-1,-1) * offset).a;
            samp += SampleSpriteTexture (uv + float2(-1,1) *offset).a;
            samp += SampleSpriteTexture (uv + float2(1,-1) *offset).a;
            samp += SampleSpriteTexture (uv + float2(1,1) * offset).a;

            return samp/5;
        }


        fixed4 SpriteFrag2(v2f IN) : SV_Target
        {
            float2 screenUV = IN.vertex.xy / _ScreenParams.xy;


            float2 lightDir = (_SunScreenPos.xy - screenUV);
            float2 scaledDeta = min(_EdgeShadowScale * length(lightDir) ,_EdgeShadowMax)*normalize(lightDir);
            

            //Edge Shadow
            float samp1 = SampleSpriteTexture5ForA(IN.texcoord - scaledDeta, _BlurDistance);
            
            //Edge Light
            float samp2 =1 - SampleSpriteTexture5ForA(IN.texcoord + scaledDeta, _BlurDistance);

            float samp = saturate(lerp(samp1, samp2,(1 + _IsShadow)*0.5));
            


            float dirFade = 1;


            //计算边缘阴影
            samp = min(dirFade,samp);
            half3 screenLight = 0;
#ifdef SCREEN_LIGHT
			half3 sampleScreenLight = tex2D(_ScreenLightTex, screenUV).rgb * 2 - 1;

			screenLight = clamp(sampleScreenLight,0,1)* (1 - _DirectWeightFade);

			samp = min(1 + clamp(sampleScreenLight, -1, 0), samp);
#endif

            fixed4 lightColor = lerp(_IndirectColor,_DirectColor * _EdgeLightMax,min(1 - _DirectWeightFade,samp));
        
            fixed4 c = SampleSpriteTexture (IN.texcoord) * IN.color * fixed4(fixed3(1,1,1) * lightColor.rgb + screenLight,1);
            c.rgb *= c.a;
            return c;
        }

        ENDCG
        }

      
	

    }   
   
}
